package vn.com.baseapplication.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.util.Base64;
import android.util.Log;

public class FBUtils {
	public static String getHashKeySentToFB(Context applicationContext,
			String appPackage) {
		String keyHash = "";
		// Add code to print out the key hash
		try {
			PackageInfo info = applicationContext.getApplicationContext()
					.getPackageManager()
					.getPackageInfo(appPackage, PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				Log.d("KeyHash:",
						Base64.encodeToString(md.digest(), Base64.DEFAULT));
				keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
			}
		} catch (NameNotFoundException e) {
			keyHash = e.getMessage().toString();
		} catch (NoSuchAlgorithmException e) {
			keyHash = e.getMessage().toString();
		}
		return keyHash;
	}
}
